#encoding = utf-8
from util.ObjectMap import *
from util.ParseConfigFile import ParseConfigFile
from playwright.sync_api import sync_playwright, Page

class LoginPage(object):

    def __init__(self, page: Page):
        self.page = page
        self.parseCF = ParseConfigFile()
        self.loginOptions = self.parseCF.getItems("login")
        print(self.loginOptions)

    def selectLoginMethod(self, text):
        if "pw" == text:
            getElements(self.page, self.loginOptions["loginPage.pwlogin".lower()]).click()
        else:
            getElements(self.page, self.loginOptions["loginPage.pflogin".lower()]).click()


    def pwEmailObj(self):
        try:
            self.selectLoginMethod('pw')
            # elementObj = getElements(self.page, "//div[@class='login-form']//input[@placeholder='Enter email']")
            elementObj = getElements(self.page, self.loginOptions["loginPage.pwemail".lower()])
            return elementObj
        except Exception as err:
            raise err

    def pfEmailObj(self):
        try:
            self.selectLoginMethod('pf')
            elementObj = getElements(self.page, self.loginOptions["loginPage.pfemail".lower()])
            return elementObj
        except Exception as err:
            raise err

    def passwordObj(self):
        try:
            elementObj = getElements(self.page, self.loginOptions["loginPage.password".lower()])
            return elementObj
        except Exception as err:
            raise err

    def verificationCodeObj(self):
        try:
            elementObj = getElements(self.page, self.loginOptions["loginPage.verificationcode".lower()])
            return elementObj
        except Exception as err:
            raise err

    def loginButtonObj(self):
        try:
            elementObj = getElements(self.page, self.loginOptions["loginPage.loginbutton".lower()])
            return elementObj
        except Exception as err:
            raise err

if __name__ == "__main__":
    p = sync_playwright().start()
    browser = p.chromium.launch(headless=False)

    # 创建 BrowserContext对象
    context = browser.new_context()
    # 启动跟踪功能
    # context.tracing.start(snapshots=True, sources=True, screenshots=True)

    page = context.new_page()
    page.goto("xxx")
    # page.wait_for_timeout(20000)
    loginPage = LoginPage(page)
    loginPage.pwEmailObj().fill("xxx")
    loginPage.passwordObj().fill("xxx")
    loginPage.loginButtonObj().click()
    page.wait_for_timeout(10000)

    browser.close()
    p.stop()
